package com.ting.test.algorithms.链表;

/**
 * 将一个链表反转
 * <p>
 * 5>4>3>2>1
 * 1>2>3>4>5
 */
public class 单链表反转 {
    public static void main(String[] args) {
        SingleNode node1 = new SingleNode(1, null);
        SingleNode node2 = new SingleNode(2, node1);
        SingleNode node3 = new SingleNode(3, node2);
        SingleNode node4 = new SingleNode(4, node3);
        SingleNode node5 = new SingleNode(5, node4);

        SingleNode node = node5;
        while (node.next != null) {
            System.out.println(node.value + ",");
        }
        System.out.println(node.value);

        SingleNode result = fund(node5);
        while (result.next != null) {
            System.out.println(result.value + ",");
        }
        System.out.println(result.value);
    }


    //a>b>c>d>e
    //e>d>c>b>a
    // 对于 c c.pre=b c.next=d
    //转换后  c.pre=d c.next=b
    private static SingleNode fund(SingleNode head) {
        SingleNode pre = null;
        SingleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }


}
